<?php
namespace app\controller;
use app\service\InstorageService,
    app\model\Product,
    app\model\Supplier,
    app\model\User,
    app\model\Order,
    app\model\Storage;

class Instorage extends Base
{
    protected $service;
    public function __construct(InstorageService $service)
    {
        parent::__construct();
        $this->service = $service;
    }  

    public function index()
    {
        $lists = $this->service->page();
        foreach ($lists as $key => $val) {
            switch ($val['check_status']){
                case 0:
                    $lists[$key]['check_status'] = '待审核';
                    break;
                case 1:
                    $lists[$key]['check_status'] = '已通过';
                    break;
                case 2:
                    $lists[$key]['check_status'] = '已驳回';
                    break;
            }
        }
        $this->assign(['list'  =>  $lists]);
        return view();
    }

    public function create()
    {
        $this->assign([
            'userList'   =>  User::all(['status' => 0 ]),
            'product'    =>  Product::all(['status' => 0 ]),
            'supplier'   =>  Supplier::all(['status' => 0 ]),
            'storage'    =>  Storage::all(['status' => 0 ]),
        ]);
        return view();
    }

    public function show($id)
    {
        $info  =  Order::get(['id'=>$id]);
        switch ($info['check_status']){
                case 0:
                    $info['check_name'] = '待审核';
                    break;
                case 1:
                    $info['check_name'] = '已通过';
                    break;
                case 2:
                    $info['check_name'] = '已驳回';
                    break;
            }
        $userName = User::get($info['author']);
        $info['author'] = $userName['truename'];
        $this->assign([
            // 'product'    =>  Product::all(  [ 'status' => 0 ] ),
            // 'supplier'   =>  Supplier::all(  [ 'status' => 0 ] ),
            'info'  =>  $info,
            'list'  => $this->service->getAllList($id)
        ]);
        return view();
    }

    public function save()
    {
        return $this->service->save();
    }

    public function edit($id, $style='')
    {
        if (empty($style)) {
            $info  =  Order::get(['id'=>$id]);
        } else {
            $info  =  Order::get(['id'=>$id]);
            switch ($info['check_status']){
                    case 0:
                        $info['check_name'] = '待审核';
                        break;
                    case 1:
                        $info['check_name'] = '已通过';
                        break;
                    case 2:
                        $info['check_name'] = '已驳回';
                        break;
                }
            $userName = User::get($info['author']);
            $info['author'] = $userName['truename'];
        }
         $this->assign([
            'style'    => $style,
            'info'     => $info,
            'product'  => Product::all(['status' => 0 ]),
            'supplier' => Supplier::all(['status' => 0 ]),
            'storage'  => Storage::all(['status' => 0 ]),
            'list'     => $this->service->getAllList($id)
        ]); 
        return view();
    }

    public function update()
    {
        return $this->service->update();
    }

    public function delete($id){
        return $this->service->delete($id);
    }
    
    public function prints($id)
    {
        $this->assign([
            'info'  =>  Order::get($id),
            'list'  => $this->service->getAllList($id)
        ]);
        return view();
    }

    public function barcode($sn){
        include ROOT_PATH.'/extend/Barcode.php';
        die( \Barcode::code39($sn) );  
    }

    public function upload(){
        return $this->service->upload();
    }
    
}
